package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import com.esotericsoftware.minlog.Log;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告控制器 - 处理系统公告管理相关的HTTP请求
 * 包括公告的增删改查、状态管理、前台展示等功能
 */
@Slf4j
@RestController  // REST控制器，返回JSON数据
@RequestMapping("/api/notices")  // 公告API路径前缀
@CrossOrigin  // 允许跨域访问
@Tag(name = "公告管理", description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")  // Swagger API分组
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping("/list")
    @Operation(summary = "获取所有公告", description = "获取所有公告列表，包括启用和禁用的，供管理后台使用")
    public Result<List<Notice>> getAllNotice() {
        Result<List<Notice>> allNotices = noticeService.getAllNotices();
        log.info("查询所有后台需要的公告信息业务执行成功！结果为：{}", allNotices);
        return allNotices;
    }

    @PutMapping("/update")
    @Operation(summary = "更新公告", description = "修改公告的内容、标题、类型等信息")
    public Result<String> updateNotice(@RequestBody Notice notice) {
        Result<String> stringResult = noticeService.updateNotice(notice);
        Log.info("公告数据修改业务成功！！");
        return stringResult;
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取公告", description = "根据公告ID获取单个公告的详细信息")
    public Result<Notice> getNoticeByID(@Parameter(description = "公告ID") @PathVariable Long id) {
        Notice byId = noticeService.getById(id);
        if (byId != null) {
            log.info("公告详情查询成功！查询结果为：{}", byId);
            return Result.success(byId, "公告详情查询成功！");
        }
        log.info("公告详情查询失败！没有查询指定到id:{}的公告信息！", id);
        return Result.error("公告详情查询失败");
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新公告", description = "获取最新发布的公告列表，用于首页推荐展示")
    public Result<List<Notice>> getNoticeLatest(@RequestParam(defaultValue = "5") int limit) {
        Result<List<Notice>> latestNotices = noticeService.getLatestNotices(limit);
        log.info("查询最新公告信息业务执行成功!结果为：{}", latestNotices);
        return latestNotices;
    }

    @GetMapping("/active")
    @Operation(summary = "获取启用的公告", description = "获取状态为启用的公告列表，供前台首页展示使用")
    public Result<List<Notice>> getNoticeActive() {
        Result<List<Notice>> activeNotices = noticeService.getActiveNotices();
        log.info("查询说有后台需要的激活状态公告信息业务执行成功！");
        return activeNotices;
    }

    @PostMapping("/add")
    @Operation(summary = "发布新公告", description = "创建并发布新的系统公告")
    public Result<String> addNotice(@RequestBody Notice notice) {
        Result<String> addNotice = noticeService.addNotice(notice);
        log.info("发布新公告信息业务执行成功！");
        return addNotice;
    }

    @PutMapping("/toggle/{id}")
    @Operation(summary = "切换公告状态", description = "启用或禁用指定的公告，禁用后不会在前台显示")
    public Result<String> toggleNoticeStatus(@Parameter(description = "公告ID")
                                             @PathVariable long id,
                                             @Parameter(description = "是否启用，true为启用，false为禁用")
                                             @RequestParam boolean isActive) {
        Result<String> stringResult = noticeService.toggleNoticeStatus(id, isActive);
        log.info("公告状态修改成功，修改后的状态为：{}", isActive);
        return stringResult;
    }

    @DeleteMapping("/deldete/{id}")
    @Operation(summary = "删除公告", description = "根据ID删除指定的公告")
    public Result<String> deleteNotice(@Parameter(description = "公告ID") @PathVariable long id) {
        Result<String> stringResult = noticeService.deleteNotice(id);
        log.info("删除公告数据成功！删除id为：{}", id);
        return stringResult;
    }
}
